﻿<?php
//*******************************************************************
//*			BulletClock用ネットランキング・エントリ					*
//*						松久貫太	2007/10/05						*
//*******************************************************************

require_once "DB.php";

class NetRanking{
	var $db;
	
	function setDb($db){
		$this->db = $db;
	}
	
	function NetRanking(){
	}
	
	function main(){
		$mode = isset($_POST['mode']) ? $_POST['mode'] : "";
		$option = isset($_POST['option']) ? $_POST['option'] : "";
		
		echo $mode;
		
		if($mode == "post"){
			// dbに書き込む
			$entry = array();
			$entry['name'] = isset($_POST['name']) ? $_POST['name'] : "noname";
			$entry['score'] = isset($_POST['score']) ? intval($_POST['score']) : 0;
			
			$ret =& $this->db->query($this->sqlInsertEntry($entry));
			echo $this->sqlInsertEntry($entry);
			if(PEAR::isError($this->db)){
				echo $ret->getMessage();
				echo "error!!!";
				return false;
			}
			
			echo "post ok";
			return true;
			
		}else if($mode == "show"){
			var_dump($this->dump());
			return true;
			
		}else if($mode == ""){
			// 未設定機能
			$result = $this->dump();
			
			echo "最近の書き込み<br>";
			echo "<table border=1>";
			foreach($result as $line){
				echo "<tr>";
				echo "<td>" . htmlspecialchars($line[0]) . "</td><td>" . $line[1] . "</td><td>" . $line[2]. "</td>";
				echo "</tr>";
			}
			echo "</table>";
			
			$result = $this->dumpscore();
			echo "ハイスコア";
			echo "<table border=1>";
			foreach($result as $line){
				echo "<tr>";
				echo "<td>" . htmlspecialchars($line[0]) . "</td><td>" . $line[1] . "</td><td>" . $line[2]. "</td>";
				echo "</tr>";
			}
			echo "</table>";
			
			return true;
		}
	}
	
	function sqlInsertEntry($entry){
		return  "insert into bulletclock_score(name,score,timestamp)"
			. " values('" . mysql_real_escape_string($entry['name']) . "',"
			. $entry['score'] . " , now() );";
	}
	
	function sqlSelectEntryByTime(){
		return "select name,score,timestamp from bulletclock_score where 1 order by timestamp desc limit 0,10;";
	}
	function sqlSelectEntryByScore(){
		return "select name,score,timestamp from bulletclock_score where 1 order by score desc limit 0,10;";
	}
	
	function dump(){
			// dbから読む
			$ret =& $this->db->query($this->sqlSelectEntryByTime());
			if(PEAR::isError($this->db)){
				echo $ret->getMessage();
				return false;
			}
			
			$result = array();
			while($ret->fetchInto($line , DB_FETCHMODE_ORDERED))
			{
				$result[] = $line;
			}
			
			return $result;
	}
	function dumpscore(){
			// dbから読む
			$ret =& $this->db->query($this->sqlSelectEntryByScore());
			if(PEAR::isError($this->db)){
				echo $ret->getMessage();
				return false;
			}
			
			$result = array();
			while($ret->fetchInto($line , DB_FETCHMODE_ORDERED))
			{
				$result[] = $line;
			}
			
			return $result;
	}
}

//-------------------------------------------------------

$dsn = array(
	'phptype' => 'mysql' ,
	'username' => '' ,
	'password' => '' ,
	'hostspec' => '' ,
	'database' => ''
	);
$db =& DB::connect($dsn);
if(PEAR::isError($db)) die($db->getMessage());

$nrank = new NetRanking();
$nrank->setDb($db);

// DEBUG
ob_start();
if(!$nrank->main()) echo "error hoge";

$filename = date("YmdHis") . ".txt";
//$fp = fopen($filename , "w+");
//var_dump($_POST);
//var_dump($_GET);
$str = ob_get_clean();
//fwrite($fp , $str);

echo "<meta http-equiv=\"Refresh\" content=\"20\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo $str;
// dump
?>
<!--
// for debug
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" value="post">
<input type="text" name="name" value="debug">
<input type="text" name="score" value="123456789">
<input type="submit">
</form>
-->